<?php

include_once ('bn_path.php');
class bn_userinfo
{
    private $dl;
    private $gf;
    private $conf;
    protected static $instance;

    function __construct()
    {
        $this->dl = new bn_dal();
        $this->gf = new bn_genralfunc;
        $this->conf = new bn_config;
    }


    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function insert($user)
    {
        $page = $_SERVER['REQUEST_URI'];
        if (strpos($page, '.php') === false)
            return null;
        $codex = $this->gf->RandomStrgenerate(16);
        $this->dl->insert('userinfo', array($user,$this->gf->Get_ip(),$page,$this->gf->date(),$this->gf->date(),$codex));
        return $codex;
    }

    public function update_user_info($codex)
    {
        $page = $_SERVER['REQUEST_URI'];
        if (strpos($page, '.php') === false)
        return null;
        $this->dl->update('userinfo', array('page' => $page, 'timeout' => $this->gf->date()), array('codex=' => $codex));
    }

    public function is_login_today($user)
    {
        if ($user == 'unknow')
        {
            $codex = $this->dl->getvalue('select codex from userinfo where user="unknow" and ip="' .$this->gf->Get_ip() . '" and timein>curdate()');
            if (isset($codex))
                return self::create_userinfo('unknow', $codex);
        } 
        else
        {
            $codex = $this->dl->getvalue('select codex from userinfo where user=? and timein>curdate()',$user);
            if (isset($codex))
                return self::create_userinfo($user, $codex);
        }
    }

    public function is_login_yesterday($user)
    {
        if ($user == 'unknow')
            return $this->dl->getvalue('select codex from userinfo where user="unknow" and ip="' .$this->gf->Get_ip() . '" and timein>(curdate()-interval 1 day)');
        else
            return $this->dl->getvalue('select codex from userinfo where user=? and timein>(curdate()-interval 1 day)',$user);
    }

    public function create_userinfo($user, $codex = '')
    {
        if ($user == 'unknow')
        {
            $arr['user'] = 'unknow';
            $arr['rol'] = '9999';
            $arr['rolname'] = 'anony';
            $arr['nikname'] = 'ناشناس';
            $arr['lang'] = $this->conf->get_default_lang();
            if ($codex == '')
                $arr['codex'] = self::insert('unknow');
            else
                $arr['codex'] = $codex;
        } 
        else
        {
            $arr = $this->dl->getrow('select user,rol,(name)rolname,nikname,lang from users u join rolname r on u.rol=r.id and user=?',$user)->fetch();
            if ($codex == '')
                $arr['codex'] = self::insert($user);
            else
                {
                    $this->dl->update('userinfo', array('user' => $user), array('codex=' => $codex));
                    $arr['codex'] = $codex;
                }
        }
        return $arr;
    }

    public function get_user_info($codex)
    {
        return $this->dl->getrow('select * from userinfo where codex=?', array($codex))->fetch();
    }

    public function select_alluinfo($col = '', $val = '')
    {
        if (empty($col) || empty($val))
            return $this->dl->select('select * from userinfo order by id desc');
        else
            return $this->dl->Select('select * from userinfo', array("$col=" => $val))->orderby("id desc");
    }

    public function Get_lastvisit($user)
    {
        return $this->dl->getrow("SELECT timeout FROM sample.userinfo where user=? order by id desc LIMIT 1",
            $user)->fetch();
    }

    function __destruct()
    {
        unset($this->dl);
        unset($this->gf);
        unset($this->conf);
    }
}

?>